import React, { Component } from 'react'
import { Tabs, Spin } from 'antd'

import request from '@/Utils/fecth'
import { address } from '@/Utils/apiConfig'

import PolicyList from './components/PolicyList'
import PrivateData from './components/PrivateData'

const { TabPane } = Tabs

const { leagueManagement: { getOrganization }} = address

class PolicyManagement extends Component {
  constructor(props) {
    super(props)
    this.state = {
      activeKey: '0',
      leagueInfo: {},
      loading: true,
      allOrgList: [],
    }
    const { id } = props.match.params
    this.consortiumId = id
  }

  componentDidMount() {
    this.getLeagueInfo()
    this.getOrgList()
  }

  async getLeagueInfo() {
    const { status, data } = await request().get(address.leagueManagement.leagueInfo.format({ consortiumId: this.consortiumId }))
    this.setState({
      loading: false,
    })
    if (status === 200) {
      this.setState({
        leagueInfo: data || {},
      })
    }
  }

  // 获取组织列表
  getOrgList = () => {
    request().get(getOrganization.format({ consortiumId: this.consortiumId })).then((res = {}) => {
      if (res.status === 200) {
        let result = res.data || []
        this.setState({
          allOrgList: result.filter(v => v.type === 'peer'),
        })
      }
    })
  }

  render() {
    const { activeKey, leagueInfo, loading, allOrgList } = this.state
    const commonProps = {
      ...this.props,
      leagueInfo,
      allOrgList,
    }
    return (
      <div className="PolicyManagement common">
        <Spin spinning={loading}>
          <div className="tabs">
            <Tabs destroyInactiveTabPane activeKey={activeKey} onChange={activeKey => this.setState({ activeKey })}>
              <TabPane tab={'背书策略管理'} key={'0'}>
                <PolicyList {...commonProps}/>
              </TabPane>
              <TabPane tab={'私有数据集策略管理'} key={'1'}>
                <PrivateData {...commonProps}/>
              </TabPane>
            </Tabs>
          </div>
        </Spin>

      </div>
    )
  }
}

export default PolicyManagement
